package com.smarthome.api.mobile.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.smarthome.base.service.ShEquipmentDdnsService;
import com.smarthome.common.mobile.Service;
import com.smarthome.core.account.AccountDoorService;
import com.smarthome.core.deviceManager.EquipmentDevTalkService;
import com.smarthome.core.housemanager.HouseDoorplatService;
import com.smarthome.domain.bean.ShAccount;
import com.smarthome.domain.bean.ShAccountDoor;
import com.smarthome.domain.bean.ShDoor;
import com.smarthome.domain.bean.ShEquipmentDdns;
import com.smarthome.domain.bean.ShEquipmentDevTalk;
import com.smarthome.domain.mobile.MobileCodeEnum;
import com.smarthome.domain.mobile.MobileRequest;
import com.smarthome.domain.mobile.MobileResponse;
import com.smarthome.domain.mobile.req.MobileDdnsRequest;

/**
 * 
* @ClassName: MobileDoorService
* @Description: 移动端门牌service
* @author cyj
* @date 2015年10月28日 下午8:09:34
*
 */
@org.springframework.stereotype.Service("mobile.door.service")
public class MobileDoorService implements Service<MobileResponse<?>> {

	@Resource
	private HouseDoorplatService houseDoorplatService;
	
	@Resource
	private ShEquipmentDdnsService shEquipmentDdnsService;
	
    @Resource
    private AccountDoorService accountDoorService;
    
    @Resource
    private EquipmentDevTalkService equipmentDevTalkService;
    
    
	/**
	 * 
	* getList
	* @Title: getList
	* @Description: 获取当前用户在当前小区下所有门牌
	* @param request
	* @param response
	* void
	* @throws
	* @author cyj
	* @date 2015年10月28日 下午8:12:47
	 */
	public void getList(MobileRequest request,MobileResponse<List<ShDoor>> response) {
		if(request.getAccount() == null || request.getAccountId() == null) {
			response.setMobileCodeObj(MobileCodeEnum.DOOR_NOT_ACCOUNT);
			return;
		}
		if(request.getAccount().getResidenceId() == null) {
			response.setMobileCodeObj(MobileCodeEnum.DOOR_NOT_RESIDENCE);
			return;
		}
		ShAccount queryAccount = new ShAccount();
		queryAccount.setId(request.getAccountId());
		queryAccount.setResidenceId(request.getAccount().getResidenceId());
		try {
			List<ShDoor> doorList = houseDoorplatService.queryDoorByAccount(queryAccount);
			response.setData(doorList);
		}catch(Exception e) {
			e.printStackTrace();
			response.setMobileCodeObj(MobileCodeEnum.SYSTEM_ERROR);
		}
	}

	/**
	 * 根据用户id和门牌号 获取ddns信息
	 * @param request
	 * @param response
	 */
	public void getDdnsList(MobileRequest request,MobileResponse<List<ShEquipmentDdns>> response) {
		if(request.getAccount() == null || request.getAccountId() == null) {//用户id
			response.setMobileCodeObj(MobileCodeEnum.DOOR_NOT_ACCOUNT);
			return;
		}
		if(request.getAccount().getResidenceId() == null) {//小区id
			response.setMobileCodeObj(MobileCodeEnum.DOOR_NOT_RESIDENCE);
			return;
		}
		if(request.getAccount().getDoorId() == null) {//门牌id
			response.setMobileCodeObj(MobileCodeEnum.DOOR_NOT_DOORID);
			return;
		}
		//根据用户id和门牌号 获取ddns信息
		ShEquipmentDdns equipmentDdns = new ShEquipmentDdns();
		equipmentDdns.setDoorId(request.getAccount().getDoorId());
		equipmentDdns.setResidenceId(request.getAccount().getResidenceId());
		try {
			List<ShEquipmentDdns> equipmentDdnsList = shEquipmentDdnsService.selectList(equipmentDdns);
			response.setData(equipmentDdnsList);
		}catch(Exception e) {
			e.printStackTrace();
			response.setMobileCodeObj(MobileCodeEnum.SYSTEM_ERROR);
		}
	}
	
	/**
	 * 注册ddns
	 * @param request
	 * @param response
	 */
	public void registerDdns(MobileDdnsRequest request,MobileResponse<List<ShEquipmentDdns>> response) {
		if(request.getAccount() == null || request.getAccountId() == null) {//用户id
			response.setMobileCodeObj(MobileCodeEnum.DOOR_NOT_ACCOUNT);
			return;
		}
		if(request.getAccount().getResidenceId() == null) {//小区id
			response.setMobileCodeObj(MobileCodeEnum.DOOR_NOT_RESIDENCE);
			return;
		}
		if(request.getBuildingId() == null) {//楼栋id
			response.setMobileCodeObj(MobileCodeEnum.NOT_BUILDINGID);
			return;
		}
		if(request.getUnitId() == null) {//单元id
			response.setMobileCodeObj(MobileCodeEnum.NOT_BUILDINGUNITID);
			return;
		}
		if(request.getAccount().getDoorId() == null) {//门牌id
			response.setMobileCodeObj(MobileCodeEnum.DOOR_NOT_DOORID);
			return;
		}
		String ddns = request.getDdns();
		if(StringUtils.isBlank(ddns)){
			response.setMobileCodeObj(MobileCodeEnum.DDNS_NOT);
			return;
		}
		
		//根据用户id和门牌号 获取ddns信息
		ShEquipmentDdns equipmentDdns = new ShEquipmentDdns();
		equipmentDdns.setDoorId(request.getAccount().getDoorId());
		equipmentDdns.setResidenceId(request.getAccount().getResidenceId());
		equipmentDdns.setBuildingId(request.getBuildingId());
		equipmentDdns.setBuildingUnitId(request.getUnitId());
		List<ShEquipmentDdns>  list = shEquipmentDdnsService.selectList(equipmentDdns);
		if(list!=null && list.size()>0){//如果有就更新
			equipmentDdns = list.get(0);
			equipmentDdns.setGmtModify(new Date());//修改时间
		}else{
			equipmentDdns.setGmtCreate(new Date());//创建时间
		}
		equipmentDdns.setResidenceId(request.getAccount().getResidenceId());//小区id
		equipmentDdns.setBuildingId(request.getBuildingId());//楼栋id
		equipmentDdns.setBuildingUnitId(request.getUnitId());//单元id
		equipmentDdns.setDoorId(request.getAccount().getDoorId());//门牌id
		equipmentDdns.setDdns(ddns);
		equipmentDdns.setStatus(1L);
		try {
			int num = shEquipmentDdnsService.saveOrUpdate(equipmentDdns);
			if(num>0){
				//通知门口机已经注册成功
				ShEquipmentDevTalk talk = new ShEquipmentDevTalk();
				talk.setResidenceId(request.getAccount().getResidenceId());//小区id
				talk.setBuildingId(request.getBuildingId());//楼栋
				List<ShEquipmentDevTalk> talkList =equipmentDevTalkService. getAllList( talk);
				if(talkList!=null && talkList.size()>0){
					talk= talkList.get(0);
					talk.setIs_ddns("1");		//是否注册了ddns；1：已经注册。0：未注册
					equipmentDevTalkService.saveOrUpdate(talk);
					response.setMobileCodeObj(MobileCodeEnum.ACCOUNT_REG_SUC);
				}else{
					response.setMobileCodeObj(MobileCodeEnum.DEVICE_NO_MKJINFO);//未查询到门口机信息
				}
			}else{
				response.setMobileCodeObj(MobileCodeEnum.DEVICE_FAIL_DO);
			}
		}catch(Exception e) {
			e.printStackTrace();
			response.setMobileCodeObj(MobileCodeEnum.SYSTEM_ERROR);
		}
	}
	
	/**
     * 
    * @Title: getArrDetailIdList
    * @Description: 获取用户住址id详细信息
    * @param request
    * @param response 
    * void  
    * @throws
     */
    public void getArrDetailIdList(MobileRequest request, MobileResponse<List<Map<String, String>>> response) {
        if(request.getAccountId() == null) {
        	response.setMobileCodeObj(MobileCodeEnum.DOOR_NOT_ACCOUNT);
        	return;
        }
        List<ShAccountDoor> list = null;
        try {
        	list = accountDoorService.getDetailList(request.getAccountId());
        }catch(Exception e) {
        	e.printStackTrace();
        	response.setMobileCodeObj(MobileCodeEnum.SYSTEM_ERROR);
        	return;
        }
        List<Map<String,String>> result = new ArrayList<Map<String,String>>();
        if(list != null && !list.isEmpty()) {
        	Map<String,String> map = null;
        	int length = list.size();
        	for(int i = 0;i < length;i++) {
        		map = new HashMap<String,String>();
        		map.put("QH", list.get(i).getResidenceId()>9?(list.get(i).getResidenceId().toString()):"0"+list.get(i).getResidenceId());//小区
        		map.put("DH", list.get(i).getBuildingId()>9?(list.get(i).getBuildingId().toString()):"0"+list.get(i).getBuildingId());//栋
        		map.put("DYH", list.get(i).getUnitId()>9?(list.get(i).getUnitId().toString()):"0"+list.get(i).getUnitId());//单元
        		map.put("CH", "01");//层 
        		map.put("FH", list.get(i).getDoorId()>9?(list.get(i).getDoorId().toString()):"0"+list.get(i).getDoorId());//房
        		result.add(map);
        	}
        }
    	response.setData(result);
    }
}
